/**
 * This file is part of the dspace-poltman project http://www.poltman.com.
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of DuraSpace DSpace license detailed in the LICENSE and NOTICE 
 * files at the root of the source tree and available online at
 * http://www.dspace.org/license/
 * 
 */
package com.poltman.dscentral.dscentral.menuPolicies;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.spring.SpringUtil;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;

import com.poltman.dscentral.db.entity.pm.MainMenuGroupsEntity;
import com.poltman.dscentral.db.service.pm.MainMenuGroupsService;
import com.poltman.dscentral.extended.services.MainMenuGroupsServiceFacade;

/**
 * 
 * @author Zbigniew Ciok; z.ciok@poltman.com
 * @version $Revision: 1670 $
 *
 */
@SuppressWarnings("rawtypes")
public class NewMenuGroupController extends GenericForwardComposer {
	
	private static final long serialVersionUID = -2362853298771961831L;
	
	private static Logger log = Logger.getLogger(NewMenuGroupController.class);
	
	private Textbox groupShortcutTextbox;
	private Textbox groupDescrTextbox;
	
	private Listbox itemsSourceListbox;
	private Listbox itemsTargetListbox;	
	
	List<Component> targetItems = null;
	

	@SuppressWarnings("unchecked")
	@Override
    public void doAfterCompose(org.zkoss.zk.ui.Component comp) throws Exception {
    	super.doAfterCompose(comp);
    }	
	
	
	/*
	 * Saves new menu group
	 */
	public void onClick$saveNewGroupBtn() {
		
		String groupShortcut = (groupShortcutTextbox.getValue() == null) ? "" : groupShortcutTextbox.getValue();
		String groupDescr = (groupDescrTextbox.getValue() == null) ? "" : groupDescrTextbox.getValue();
		
		if ((groupShortcut.isEmpty()) || (groupDescr.isEmpty())) {
			//TODO: i18n
			Messagebox.show("Insert Group Id AND Group Description", "DSCentral Error", 0,  Messagebox.ERROR);
			return;
		}
		
		try {
			MainMenuGroupsService mainMenuGroupService = (MainMenuGroupsService) SpringUtil.getBean("mainMenuGroupsService");
			List<MainMenuGroupsEntity> groups = mainMenuGroupService.findByShortcut(groupShortcut);
			if ((groups != null) && (groups.size() >= 1)) {
				//TODO: i18n
				Messagebox.show("Group already exists", "DSCentral Error", 0,  Messagebox.ERROR);
				return;
			}
		} catch (Exception e) {
			log.error(e);
			//TODO: i18n
			Messagebox.show("Group search error", "DSCentral Error", 0,  Messagebox.ERROR);
			return;
		}
		
		this.targetItems = itemsTargetListbox.getChildren();
		List<MainMenuGroupsEntity> mainMenuGroup = new ArrayList<MainMenuGroupsEntity>();
		if (this.targetItems.size() < 1) {
			//TODO: i18n
			Messagebox.show("Nothing to save error...", "DSCentral Error", 0,  Messagebox.ERROR);
			return;
		} else {
			for (int i = 0; i < this.targetItems.size(); i++) {
				String menuItem = ((Listcell)(this.targetItems.get(i).getFirstChild())).getLabel();
				
				MainMenuGroupsEntity groupItem = new MainMenuGroupsEntity();
				groupItem.setShortcut(groupShortcutTextbox.getValue());
				groupItem.setMenuItem(menuItem);
				groupItem.setDescr(groupDescrTextbox.getValue());
				
				mainMenuGroup.add(groupItem);
			}	
			
			try {
				MainMenuGroupsServiceFacade mainMenuGroupsFacade = (MainMenuGroupsServiceFacade) SpringUtil.getBean("mainMenuGroupsServiceFacade");
				mainMenuGroupsFacade.addMainMenuGroup(mainMenuGroup);
			} catch (Exception e) {
				log.error("Save group error...", e);
				Messagebox.show("Save group error...", "DSCentral Error", 0,  Messagebox.ERROR);
				return;
			}
		}
		
		try {
			this.resetAndClearLists();
		} catch (Exception e) {
			log.error("resetAndClearLists error. ", e);
			Messagebox.show("Lists clear error...", "DSCentral", 0,  Messagebox.INFORMATION);
			return;
		}
		
		Messagebox.show("Group " + groupShortcut + " has been saved.", "DSCentral", 0,  Messagebox.INFORMATION);
	}	
	
	/**
	 * Clears and reloads menu items
	 * @param items
	 */
	private void resetAndClearLists() throws Exception {
		
		//clears target items
		this.targetItems.clear();
		
		//clears source items 
		List<Component> sourceItems = this.itemsSourceListbox.getChildren();
		sourceItems.clear();
		
		//loads source itmes
		MenuItemsVM model = new MenuItemsVM();
		this.itemsSourceListbox.setModel(model.getMainMenuLabels());
		
		
		//clears group shortcut and description
		this.groupShortcutTextbox.setValue("");
		this.groupDescrTextbox.setValue("");
	}
	
	/**
	 * Resets menu items
	 */
	public void onClick$resetItemsBtn() {

		//clears target items
		this.targetItems = itemsTargetListbox.getChildren();
		this.targetItems.clear();
		
		//loads source itmes
		MenuItemsVM model = new MenuItemsVM();
		
		try {
			this.itemsSourceListbox.setModel(model.getMainMenuLabels());
		} catch (Exception e) {
			log.error("onClick$resetItemsBtn()", e);
			Messagebox.show("Reset items error.", "DSCentral Error", 0,  Messagebox.ERROR);
		}
	}
}
